package tripx.util;

import java.util.ArrayList;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

public class JsonHelper {

	private static Gson gson;

	private static JsonParser parser;

	public static <T> T parseSingle(String jsonStr, Class<T> classOfT) {
		return gson.fromJson(jsonStr, classOfT);
	}

	public static <T> ArrayList<T> parseList(String jsonStr, Class<T> classOfT) {
		ArrayList<T> tList = new ArrayList<T>();
		JsonArray array = parser.parse(jsonStr).getAsJsonArray();

		for (JsonElement ele : array) {
			tList.add(gson.fromJson(ele, classOfT));
		}

		return tList;
	}

	public static String toJson(Object src) {
		return gson.toJson(src);
	}
}
